import receiverMgr from '@/biz/receiver/receiver'

let _this = null

function config(key1, key2, key3, key4) {
  let arr = [
    {
      col: 12,
      title: '呼入号归属地',
      key: key1,
      load: (o) => {
        receiverMgr.getCityDictionaries().then((res) => {
          let arr = []
          res &&
            res.forEach((item) => {
              let obj = {}
              obj.key = item
              obj.value = item
              arr.push(obj)
            })
          o.select.options = arr
        })
      },
      select: {
        options: [],
        clearable: true
      },
      change: () => {
        _this.info[key2] = ''
        getPhoneByCity({ page: 1, rows: 10, city: _this.info[key1] }).then((res) => {
          arr[1].group[0].select.options = res.list
        })
      }
    },
    {
      col: 12,
      title: '呼入号码',
      key: key2,
      group: [
        {
          title: '',
          key: key2,
          load: (o) => {
            console.log(o, '1111')
            let obj = {}
            if (_this.info[key1]) {
              if (_this.info[key2]) {
                obj = { key: _this.info[key2], value: _this.info[key2] }
                o.select.options.unshift(obj)
              }
              getPhoneByCity({ page: 1, rows: 10, city: _this.info[key1] }).then((res) => {
                o.select.options = [obj, ...res.list]
              })
            }
          },
          select: {
            options: []
          }
        },
        {
          title: '',
          button: {
            type: 'text',
            click: (data) => {
              if (!_this.info[key1]) return false
              _this.info[key2] = ''
              ++_this[key4].page
              if (_this[key4].pages < _this[key4].page) {
                _this[key4].page = 1
              }
              let params = {
                page: _this[key4].page,
                rows: _this[key4].rows,
                city: _this.info[key1]
              }
              getPhoneByCity(params).then((res) => {
                console.log(res)
                _this.$Message.success('已更换')
                _this[key4].pages = res.pages
                arr[1].group[0].select.options = res.list
              })
            },
            style: {
              color: '#2d8cf0'
            },
            title: '换一批'
          }
        }
      ]
    }
  ]
  if (_this.info[key3]) {
    return arr
  } else {
    return []
  }
}

function receiverFun() {
  let arr = [
    {
      title: '顾问姓名',
      key: 'userId',
      load: (o) => {
        receiverMgr.getTeamUserByRole({ buildingId: _this.info.buildingId }).then((res) => {
          res &&
            res.forEach((item) => {
              item.key = item.userId
              item.value = item.userName
            })
          o.select.options = res
        })
      },
      select: {
        filterable: true,
        options: []
      },
      change: (data) => {
        let userInfo = data.option.select.options.filter((item) => item.userId == data.value)
        _this.info.phone = userInfo[0].phone
        _this.getInit(userInfo[0])
      },
      rules: [
        {
          required: true,
          message: '请选择顾问',
          trigger: 'blur'
        }
      ]
    },
    {
      title: '承接线索类型',
      key: 'clueType',
      checkbox: {
        options: [
          {
            key: '1',
            value: '电话',
            disabled: false
          },
          {
            key: '2',
            value: '表单',
            disabled: false
          },
          {
            key: '4',
            value: '仅外呼',
            disabled: false
          }
        ]
      },
      load: (o) => {
        let val = _this.info.clueType
        let options = arr[1].checkbox.options
        if (val.length) {
          if ((val.indexOf('1') != -1 || val.indexOf('2') != -1) && val.indexOf('4') == -1) {
            options[0].disabled = false
            options[1].disabled = false
            options[2].disabled = true
          } else {
            options[0].disabled = true
            options[1].disabled = true
            options[2].disabled = false
          }
        } else {
          options[0].disabled = false
          options[1].disabled = false
          options[2].disabled = false
        }
      },
      change: (data) => {
        let val = data.value
        let options = arr[1].checkbox.options
        if (val.length) {
          if ((val.indexOf('1') != -1 || val.indexOf('2') != -1) && val.indexOf('4') == -1) {
            options[0].disabled = false
            options[1].disabled = false
            options[2].disabled = true
            _this.info.tospurVisible = _this.hfFlag
            _this.info.baiduVisible = _this.bdFlag
            _this.updateOpt()
          } else {
            options[0].disabled = true
            options[1].disabled = true
            options[2].disabled = false
            _this.info.tospurVisible = false
            _this.info.baiduVisible = false
            _this.info.tcCity = ''
            _this.info.myTcInPhoneX = ''
            _this.info.bdCity = ''
            _this.info.myBdInPhoneX = ''
            _this.updateOpt()
          }
        } else {
          options[0].disabled = false
          options[1].disabled = false
          options[2].disabled = false
          _this.info.tospurVisible = _this.hfFlag
          _this.info.baiduVisible = _this.bdFlag
          _this.updateOpt()
        }
      },
      rules: [
        {
          required: true,
          type: 'array',
          message: '请选择承接类型',
          trigger: 'blur'
        }
      ]
    }
  ]
  return arr
}

function getPhoneByCity(data) {
  let params = {
    ...data
  }
  console.log(params)
  if (params.city) {
    return receiverMgr.getPhoneByCity(params).then((res) => {
      res.list &&
        res.list.forEach((item) => {
          item.key = item.phoneX
          item.value = item.phoneX
        })
      return res
    })
  } else {
    return Promise.resolve([])
  }
}

function callOutConfig() {
  let arr = [
    {
      col: 12,
      title: '呼出号归属地',
      key: 'outCity',
      load: (o) => {
        receiverMgr.getCityDictionaries().then((res) => {
          let arr = []
          res &&
            res.forEach((item) => {
              let obj = {}
              obj.key = item
              obj.value = item
              arr.push(obj)
            })
          o.select.options = arr
        })
      },
      select: {
        options: []
      },
      change: (data) => {
        _this.info.outPhoneX = ''
        getPhoneByCity({ page: 1, rows: 10, city: _this.info.outCity }).then((res) => {
          if (_this.info.outPhoneX) {
            let obj = { key: _this.info.outPhoneX, value: _this.info.outPhoneX }
            res.list.unshift(obj)
          }
          arr[1].group[0].select.options = res.list
        })
      },
      rules: [
        {
          required: true,
          message: '请选择呼出号归属地',
          trigger: 'blur'
        }
      ]
    },
    {
      col: 12,
      title: '呼出隐私号',
      key: 'outPhoneX',
      group: [
        {
          title: '',
          key: 'outPhoneX',
          load: (o) => {
            let obj = {}
            if (_this.info.outCity) {
              if (_this.info.outPhoneX) {
                obj = { key: _this.info.outPhoneX, value: _this.info.outPhoneX }
                o.select.options.unshift(obj)
              }
              getPhoneByCity({ page: 1, rows: 10, city: _this.info.outCity }).then((res) => {
                o.select.options = [obj, ...res.list]
              })
            }
          },
          select: {
            options: []
          }
        },
        {
          title: '',
          button: {
            type: 'text',
            click: (data) => {
              if (!_this.info.outCity) return false
              _this.info.outPhoneX = ''
              ++_this.callOutPage.page
              if (_this.callOutPage.pages < _this.callOutPage.page) {
                _this.callOutPage.page = 1
              }
              let params = {
                page: _this.callOutPage.page,
                rows: _this.callOutPage.rows,
                city: _this.info.outCity
              }
              getPhoneByCity(params).then((res) => {
                _this.$Message.success('已更换')
                _this.callOutPage.pages = res.pages
                arr[1].group[0].select.options = res.list
              })
            },
            style: {
              color: '#2d8cf0'
            },
            title: '换一批'
          }
        }
      ],
      rules: [
        {
          required: true,
          message: '请选择呼出号',
          trigger: 'change,blur'
        }
      ]
    }
  ]
  return arr
}

function options(vm) {
  _this = vm
  let arr = [
    {
      title: '',
      key: 'receiverMsg',
      text: {
        fontSize: '15px',
        fontWeight: 'bold'
      }
    },
    receiverFun(),
    callOutConfig(),
    {
      title: '',
      visible: _this.info.tospurVisible,
      key: 'tospurLabel',
      text: {
        fontSize: '15px',
        color: '#F59A23',
        fontWeight: 'bold'
      }
    },
    config('tcCity', 'myTcInPhoneX', 'tospurVisible', 'callInTcPage'),
    {
      title: '',
      visible: _this.info.baiduVisible,
      key: 'baiduLabel',
      text: {
        fontSize: '15px',
        color: '#F59A23',
        fontWeight: 'bold'
      }
    },
    config('bdCity', 'myBdInPhoneX', 'baiduVisible', 'callInBdPage')
  ]

  return arr
}

export default {
  options
}
